package com.hkrt.demo.domain.interactor

import android.text.TextUtils
import com.hkrt.demo.Const
import com.hkrt.demo.data.enums.EquipmentEnum
import com.hkrt.demo.data.pref.Preferences
import com.hkrt.demo.data.response.LoginQpos
import com.hkrt.demo.data.response.LoginResponse
import com.hkrt.demo.di.scope.PerActivity
import com.hkrt.demo.domain.entity.Like
import com.hkrt.demo.domain.exception.ApiException
import com.hkrt.demo.domain.repository.HkrtRepository
import io.reactivex.Flowable
import javax.inject.Inject

/**
 * @author  陈金广
 * @date 2018/4/4.
 */
@PerActivity
class LoginInteractor @Inject constructor( val hkrtRepository: HkrtRepository,
                                           val preferences: Preferences)  {

    var shotLikeCount: Int = 0


    fun getShotLikes(shotId: String): Flowable<List<Like>> {
        return hkrtRepository.getShotLikes(shotId)
                .doOnNext { shotLikeCount = it.count() }
    }

    fun likeShot(shotId: String) = hkrtRepository.likeShot(shotId)

    fun login(params: HashMap<String, String>) :Flowable<LoginResponse>{
        return hkrtRepository.login(params).doOnNext {
            if (it.code.equals("00000")) {
                preferences.setUserLoggedIn()
                saveUserInfo(it)
            } else {
                throw  ApiException(it.code, it.msg)
            }
        }
    }

    private fun saveUserInfo(respone: LoginResponse){
        preferences.setSharedPreferencesData("phone", respone.phone)
        var merchantStatus: String? = null
        var terminalType: LoginQpos? = null
        if (Const.TERMINAL_TYPE_QPOS.equals(respone.flag)) {
            merchantStatus = respone.qpos?.merchantStatus
            terminalType = respone.qpos
            preferences.setSharedPreferencesData("qposorqsFlag", "qposFlag")
        } else if (Const.TERMINAL_TYPE_QPAY.equals(respone.flag)) {
            merchantStatus = respone.qpay?.merchantStatus
            terminalType = respone.qpay
            preferences.setSharedPreferencesData("qposorqsFlag", "qsFlag")
            preferences.setSharedPreferencesData("terminaltype", EquipmentEnum.QS.terminaltype)
        }
        if (merchantStatus != null && terminalType != null) {
            preferences.setSharedPreferencesData(Const.MERCHANT_STATUS, merchantStatus)
//            val isFlashNetIn = terminalType!!.getIsFlashNetIn()
            preferences.setSharedPreferencesData(Const.MERCHANT_CN_NAME, terminalType.merchantCnName)
            preferences.setSharedPreferencesData(Const.BANK_ACCOUNT, terminalType.bankAccount)
            preferences.setSharedPreferencesData(Const.MERCHANT_NO, terminalType.merchantNo)
            preferences.setSharedPreferencesData(Const.LINK_PERSON, terminalType.linkPerson)
            preferences.setSharedPreferencesData(Const.SETTLE_MODE, terminalType.settleMode)
            preferences.setSharedPreferencesData(Const.BUSI_STATUS, terminalType.busiStatus)
            preferences.setSharedPreferencesData(Const.BANK_NAME, terminalType.bankName)
            preferences.setSharedPreferencesData(Const.OPENT_METHOD, terminalType.opentMethod)
            preferences.setSharedPreferencesData(Const.BANK_CODE, terminalType.bankCode)
            preferences.setSharedPreferencesData(Const.CREDENTIAL_CODE, terminalType.credentialCode)
            preferences.setSharedPreferencesData(Const.MIN_AMOUNT_LIMIT, terminalType.minAmountLimit)
            preferences.setSharedPreferencesData(Const.MERCHANT_TYPE, terminalType.merchantType)
            preferences.setSharedPreferencesData(Const.INVOICE_PRINT_NAME, terminalType.invoicePrintName)
            preferences.setSharedPreferencesData(Const.IS_QUICK_PAYMENT, if (TextUtils.isEmpty(terminalType.isQuickPayment)) "0" else terminalType.isQuickPayment)
            preferences.setSharedPreferencesData(Const.THBSYSTEMCONFIG, terminalType.thbSwitch)

        }
    }
}